using System;
using System.Collections.Generic;
using System.Text;
using Xyne.Actions;
using Xyne.Regions;
using Xyne.Utilities;
using GarageGames.Torque.Core;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Net;
using GarageGames.Torque.T2D;

namespace Xyne.Templates.BattleEffects
{
    public class RepeatBlizzardEffect : BattleEffect
    {
        #region Constants
        private const string ObjectName = "RepeatBlizzardEffect";
        #endregion

        #region Members
        private int numTimes;
        private RepeatHealthAction repeatHealthAction;
        private BattleRegionImpl battleRegion;
        private GameObjects target;
        private bool isConstant = false;
        #endregion

        #region Properties
        public Boolean IsConstant
        {
            get { return isConstant; }
            set { isConstant = value; }
        }
        #endregion

        #region Template
        private static RepeatBlizzardEffect template;

        public static RepeatBlizzardEffect Template
        {
            get
            {
                if (template == null)
                {
                    template = new RepeatBlizzardEffect();
                }
                return template;
            }
        }
        #endregion

        #region Constructor
        public RepeatBlizzardEffect()
        {
            IsTemplate = true;
            Name = ObjectName;

            CreateWithWorldLimit = true;
            AnimationData = AnimationManager.Instance.GetAnimationData("blizzardANIM");
            Layer = Game.EffectLevel;
            ObjectType = TorqueObjectDatabase.Instance.GetObjectType(Name);

            Size = new Vector2(70,95);
            EffectType = BattleEffectType.RepeatBlizzardEffect;
        }

        public void Init(RepeatHealthAction inRepeatHealthAction, BattleRegionImpl inBattleRegion, GameObjects inTarget)
        {
            AnimationData.AnimationDuration = inRepeatHealthAction.SecondsPerTick;
            repeatHealthAction = inRepeatHealthAction;
            numTimes = repeatHealthAction.NumTicks;
            battleRegion = inBattleRegion;
            target = inTarget;

            battleRegion.ModifyHealth(target, repeatHealthAction.HealthChange);
        }
        #endregion

        public void Update(PacketReader reader)
        {
            WarpToPosition(reader.ReadVector2(), 0);
        }

        #region Overrides
        public override void StartEffect()
        {
            base.StartEffect();
            OnAnimationEnd = new OnAnimationEndDelegate(AnimationEnded);
        }

        public override void Serialize(Microsoft.Xna.Framework.Net.PacketWriter writer)
        {
            writer.Write(Position);
        }
        #endregion

        #region Events
        public void AnimationEnded()
        {
            if (!isConstant)
            {
                battleRegion.ModifyHealth(target, repeatHealthAction.HealthChange);
                if (numTimes == 0)
                {
                    BattleEffectManager.Instance.RemoveBattleEffect(this);
                    MarkForDelete = true;
                }
                else
                {
                    SetAnimationFrame(0);
                    ResumeAnimation();
                    numTimes--;
                }
            }
            else
            {
                SetAnimationFrame(0);
                ResumeAnimation();
            }
        }
        #endregion

    }
}
